<template>
  <div>
    御剑乘风来,除魔天地间!import 导入文件
    <upload-excel :on-success="handleSuccess" />
  </div>
</template>

<script>
import { importEmployee } from '@/api/employees'
export default {
  name: '',
  methods: {
    handleSuccess({ header, results }) {
      // console.log(excelData, '我是回调函数')
      // 如果是导入员工
      const userRelations = {
        入职日期: 'timeOfEntry',
        手机号: 'mobile',
        姓名: 'username',
        转正日期: 'correctionTime',
        工号: 'workNumber'
      }
      var arr = []
      results.forEach(item => {
        var obj = {}
        Object.keys(userRelations).forEach(key => {
          //  英文   userRelations[key]   中文  key
          //

          if (
            userRelations[key] === 'timeOfEntry' ||
            userRelations[key] === 'correctionTime'
          ) {
            obj[userRelations[key]] = this.formatDate(item[key])
          } else {
            obj[userRelations[key]] = item[key]
          }
        })
        arr.push(obj)
      })
      // console.log(arr, 555666)
      this.importExcel(arr)
    },
    async importExcel(arr) {
      await importEmployee(arr)
      this.$router.push('/employees')
    },
    formatDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() + ''
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return (
        year +
        '-' +
        (month < 10 ? '0' + month : month) +
        '-' +
        (date < 10 ? '0' + date : date)
      )
    }
  }
}
</script>
<style lang="less" scoped></style>
